﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CityGamePlay;
using CityGatway;
using CityGateway.Objects;

namespace CityServer
{
    public partial class MetropolineView : Form
    {
        Life Life;
        Timer RefreshRate;
        public MetropolineView()
        {
            Life = new Life();
            Life.Evac += new Life.OnEvac(Life_Evac);
            Life.Immigrate += new Life.OnImmigrate(Life_Immigrate);

            InitializeComponent();

            RefreshRate = new Timer();

            RefreshRate.Interval = Life.LifeHourInterval;
            RefreshRate.Tick += new EventHandler(RefreshRate_Tick);
            RefreshRate.Start();

            this.LifeGrid.SelectedObject = Life;

        }

        void Life_Immigrate(long userid, Dictionary<int, int> immigrants)
        {
            CityView cityView = (CityView)CityList.Controls.Find(userid.ToString(), false).FirstOrDefault();
            if(cityView!=null)
                    cityView.UpdateImmigrants(immigrants);
        }

        void Life_Evac(long userid,Dictionary<int, int> evacs)
        {
            CityView cityView = (CityView)CityList.Controls.Find(userid.ToString(), false).FirstOrDefault();
            if(cityView!=null)
                    cityView.UpdateEvacs(evacs);
        }

     

        void RefreshRate_Tick(object sender, EventArgs e)
        {
            RefreshRate.Stop();
             List<City>  cities = Life.Cities.Values.ToList();


            foreach(City city in cities)
            {

       
                CityView cityView = (CityView)CityList.Controls.Find(city.UserID.ToString(), false).FirstOrDefault();

                if (cityView == null)
                {
                    cityView = new CityView();
                    cityView.City = city;
                    cityView.Name = city.UserID.ToString();
                    CityList.Controls.Add(cityView);
                    continue;

                }
 
                cityView.City = city;
                cityView.Name = city.UserID.ToString();
                cityView.Bind();
            }
            RefreshRate.Start();

        }

        private void MetropolineView_Load(object sender, EventArgs e)
        {
            foreach(City city in Life.Cities.Values)
            {
                CityView cityView = new CityView();
                cityView.City = city;
               
                cityView.Name = city.UserID.ToString();
                CityList.Controls.Add(cityView);

                //UserID.Text = city.UserID.ToString();
                //CityName.Text = city.CityName;
               
            }

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void LifeInterval_Scroll(object sender, EventArgs e)
        {
            Life.LifeInterval = Life.LifeHourInterval/ ((LifeInterval.Value) * 100);


            RefreshRate.Stop();
            RefreshRate.Interval = Life.LifeInterval;           
            RefreshRate.Start();

        }
    }
}
